import 'dart:collection';

import '../model/QueryShoppingCartBean.dart';
import 'BaseRes.dart';
import 'HttpHelper.dart';

class ShoppingOrderService {
  static final ShoppingOrderService _singleton = ShoppingOrderService._internal();

  factory ShoppingOrderService() {
    return _singleton;
  }

  ShoppingOrderService._internal();

  //查询购物车
  static const String _QUERY_SHOPPING_CART = "/ShoppingOrder/GetStore/queryShoppingCart";

  // 删除购物车商品
  static const String _DELETE_SHOPPING_CART_GOODS = "/ShoppingOrder/GetStore/batchDeleteGoods";

  // 添加购物车
  static const String _ADD_CART_GOODS = "/ShoppingOrder/GetStore/insertShoppingCart";

  static Future<List<QueryShoppingCartBean>?> queryShoppingCartList({Function(BaseRes v)? callback}) async {
    var data = await HttpHelper.getInstance()?.postHttp<List<dynamic>>(_QUERY_SHOPPING_CART, null, isShowMeg: false);
    callback?.call(data as BaseRes);
    return data?.data.map((e) => QueryShoppingCartBean.fromJson(e)).toList();
  }

  static Future<int?> deleteCartGoodsNet(List<String> goods) async {
    var params = HashMap<String, dynamic>();
    params["goodsId"] = goods;
    var data = await HttpHelper.getInstance()?.postHttp<Map<String, dynamic>>(_DELETE_SHOPPING_CART_GOODS, params);
    return data?.code;
  }

  static Future<int?> addCartGoodsNet(int goodsId, int goodsNum) async {
    var params = HashMap<String, dynamic>();
    params["goodsId"] = goodsId;
    params["goodsNum"] = goodsNum;
    var data = await HttpHelper.getInstance()?.postHttp<Map<String, dynamic>>(_ADD_CART_GOODS, params);
    return data?.code;
  }
}
